/*
 * Copyright (C) 2004, Velocityme Partnership
 * SystemPropertiesActions.java
 *
 * Created on 08 October 2004, 08:04
 */

package com.velocityme.www.interfaces.nodeactions;

import com.velocityme.session.KeyLocal;
import com.velocityme.session.SystemPropertiesLocal;
import com.velocityme.www.actionforms.SystemPropertyActionForm;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author  Robert
 */
public class SystemPropertiesActions extends NodeActions {

    /** Creates a new instance of SystemPropertiesActions */
    public SystemPropertiesActions() {
    }

    @Override
    public ActionForward show(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        request.setAttribute("selectedTab", "SystemProperties");
        return super.show(mapping, form, request, response);
    }

    @Override
    public ActionForward showNode(ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        session.setAttribute("systemProperties", lookupSystemPropertiesBean().findAllSystemProperties(key));
        return mapping.findForward("ACTION_DISPLAY");
    }

    @Override
    public ActionForward showEdit(ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        SystemPropertyActionForm systemPropertyAF = (SystemPropertyActionForm)form;
        systemPropertyAF.setSystemProperties(lookupSystemPropertiesBean().findAllSystemProperties(key));
        session.setAttribute("systemPropertyAF", systemPropertyAF);
        return mapping.findForward("ACTION_EDIT");
    }

    @Override
    public ActionForward edit(ActionMapping mapping,
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeyLocal key = (KeyLocal)session.getAttribute("sessionKey");
        SystemPropertyActionForm actionForm = (SystemPropertyActionForm)form;
        if (request.getParameter("btnSubmit").equals("OK")) {
            lookupSystemPropertiesBean().setSystemProperties(key, actionForm.getSystemProperties());
        }
        Long nodeId = (Long)session.getAttribute("nodeId");
        request.setAttribute("nodeId", nodeId);
        return mapping.findForward("ACTION_SUCCESS");
    }

    private SystemPropertiesLocal lookupSystemPropertiesBean() {
        try {
            Context c = new InitialContext();
            return (SystemPropertiesLocal) c.lookup("java:comp/env/SystemPropertiesBean");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}
